﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using StructureMap;

namespace EnetaMvc.Web.Extensions
{
    public static class EnetaDataExtensions
    {
        public static IEnumerable<SelectListItem> GetMyRolesWithSelection(this HtmlHelper helper, IList<MyRoleDto> selectedRoles)
        {
            var selectListItem = new List<SelectListItem>();

            if (selectedRoles == null)
                selectedRoles = new List<MyRoleDto>();

            var userService = ObjectFactory.GetInstance<IUserService>();
            var myRoles = userService.ListMyRoles();
            var convertQuery = from m in myRoles
                               orderby m.Name
                               select new SelectListItem 
                                      { 
                                          Value = m.Id.ToString(), 
                                          Text = m.Name,
                                          Selected = (selectedRoles.FirstOrDefault(r => r.Id == m.Id) != null)
                                      };
            return convertQuery.ToList();
        }

        public static IEnumerable<SelectListItem> GetPublishStatesWithSelection(long selectedValue)
        {
            return GetPublishStatesWithSelection(null, selectedValue);
        }

        public static IEnumerable<SelectListItem> GetPublishStatesWithSelection(this HtmlHelper helper, long selectedValue)
        {
            var items = new List<SelectListItem>();

            var contentService = ObjectFactory.GetInstance<IContentService>();
            var states = contentService.ListPublishStates();

            var convertQuery = from s in states
                               orderby s.Name
                               select new SelectListItem
                               {
                                   Value = s.Id.ToString(),
                                   Text = s.Name,
                                   Selected = (s.Id == selectedValue)
                               };

            return convertQuery.ToList();
        }

        public static IList<UserDto> GetLatelyJoinedUsers(this HtmlHelper helper, int count)
        {
            var userService = ObjectFactory.GetInstance<IUserService>();

            return userService.ListLatelyJoinedUsers(count);
        }

        public static IList<BlogDto> GetPublishedBlogs(this HtmlHelper helper)
        {
            var newsService = ObjectFactory.GetInstance<INewsService>();

            return newsService.ListPublishedBlogs().OrderBy(b => b.Name).ToList();
        }

        public static IList<TagDto> GetTags(this HtmlHelper helper)
        {
            var contentService = ObjectFactory.GetInstance<IContentService>();

            return contentService.ListTags();
        }

        public static IList<EventListDto> GetLatestEvents(this HtmlHelper helper, int count = 5)
        {
            var eventService = ObjectFactory.GetInstance<IEventService>();

            return eventService.ListPublicEvents(1, count).Results;
        }
    }
}